package com.bootdo.system.service.impl;

import com.bootdo.common.domain.PageDO;
import com.bootdo.common.utils.*;
import com.bootdo.system.dao.FileAuditDao;
import com.bootdo.system.dao.FileInfoDao;
import com.bootdo.system.dao.UserFileDao;
import com.bootdo.system.domain.FileAuditDO;
import com.bootdo.system.domain.FileInfoDO;
import com.bootdo.system.domain.ReportDo;
import com.bootdo.system.domain.UserFileDO;
import com.bootdo.system.service.DeptService;
import com.bootdo.system.service.FileAuditService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class FileAuditServiceImpl implements FileAuditService {

	@Autowired
	private FileAuditDao fileAuditDao;

	@Autowired
	private UserFileDao userFileDao;

	@Autowired
	private FileInfoDao fileInfoDao;

	@Autowired
	DeptService deptService;

	@Override
	public FileAuditDO get(Integer id){
		FileAuditDO fileAuditDO = fileAuditDao.get(id);
		if(null != fileAuditDO && null != fileAuditDO.getFilesize()) {
			fileAuditDO.setFilesizeStr(UploadUtils.getFileSizePatter(fileAuditDO.getFilesize()));
		}
		return fileAuditDO;
	}
	
	@Override
	public List<FileAuditDO> list(Map<String, Object> map){
		List<FileAuditDO> list = fileAuditDao.list(map);
		if(!CollectionUtils.isEmpty(list)) {
			for(FileAuditDO data : list) {
				data.setFilesizeStr(UploadUtils.getFileSizePatter(data.getFilesize()));
			}
		}
		return list;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return fileAuditDao.count(map);
	}
	
	@Override
	public int save(FileAuditDO audit){
		return fileAuditDao.save(audit);
	}
	
	@Override
	public int update(FileAuditDO fileAuditDO){
		int count = 0;
		if(StringUtils.equals("1",fileAuditDO.getAuditstate())) {
			//审批通过，往userFile表中新增记录
			/*//根据uuid查询fileInfo信息
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("id",fileAuditDO.getFileid());
			FileInfoDO fileInfoDO = fileInfoDao.getFileInfoDetail(map);
			//根据uuid查询fileUser表数据
			Map<String,Object> map1 = new HashMap<String, Object>();
			map1.put("fileuuid",fileInfoDO.getUuid());*/
			UserFileDO userFileDO = userFileDao.get(fileAuditDO.getFileid());
			//新增人即为接收人
			userFileDO.setUserid(Long.valueOf(fileAuditDO.getTargetid()));
			userFileDO.setAddtime(new Date());
			// 获取request
			HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
			userFileDO.setIp(IPUtils.getIpAddr(request));
			//是否是接收文件（0:否，1：是）
			userFileDO.setIsreceive(1);
			count += userFileDao.save(userFileDO);
			fileAuditDO.setFileid(userFileDO.getId());
		}
		fileAuditDO.setAudittime(new Date());
		count += fileAuditDao.update(fileAuditDO);
		return count;
	}
	
	@Override
	public int remove(Integer id){
		return fileAuditDao.remove(id);
	}
	
	@Override
	public int batchRemove(Integer[] ids){
		return fileAuditDao.batchRemove(ids);
	}

	@Override
	public List<FileAuditDO> listLog(Map<String, Object> map){
		return fileAuditDao.listLog(map);
	}

	@Override
	public int listLogConut(Map<String, Object> map){
		return fileAuditDao.listLogConut(map);
	}

	@Override
	public FileAuditDO getCountAudit(Long auditid){
		return fileAuditDao.getCountAudit(auditid);
	}

	@Override
	public int getCountAuditByTime(String time,Long auditid,int auditstate){
		return fileAuditDao.getCountAuditByTime(time,auditid,auditstate);
	}

	@Override
	public PageDO<FileAuditDO> getAuditLogList(Query query){
		int total = fileAuditDao.getAuditLogCount(query);
		List<FileAuditDO> logs = fileAuditDao.getAuditLogList(query);
		PageDO<FileAuditDO> page = new PageDO<>();
		page.setTotal(total);
		page.setRows(logs);
		return page;
	}

	@Override
	public List<FileAuditDO> getFiletypeCount(Long auditid){
		return fileAuditDao.getFiletypeCount(auditid);
	}

	@Override
	public List<ReportDo> reportList(Map<String, Object> map){
		String deptId = map.get("deptId").toString();
		if (StringUtils.isNotBlank(deptId)) {
			Long deptIdl = Long.valueOf(deptId);
			List<Long> childIds = deptService.listChildrenIds(deptIdl);
			childIds.add(deptIdl);
			map.put("deptId", null);
			map.put("deptIds",childIds);
		}
		return fileAuditDao.reportList(map);
	}

	@Override
	public int reportCount(Map<String, Object> map){
		String deptId = map.get("deptId").toString();
		if (StringUtils.isNotBlank(deptId)) {
			Long deptIdl = Long.valueOf(deptId);
			List<Long> childIds = deptService.listChildrenIds(deptIdl);
			childIds.add(deptIdl);
			map.put("deptId", null);
			map.put("deptIds",childIds);
		}
		return fileAuditDao.reportCount(map);
	}

	@Override
	public List<ReportDo> typeReportList(Map<String, Object> map){
		return fileAuditDao.typeReportList(map);
	}

	@Override
	public int typeReportCount(Map<String, Object> map){
		return fileAuditDao.typeReportCount(map);
	}

	@Override
	public List<ReportDo> auditReportList(Map<String, Object> map){
		String deptId = map.get("deptId").toString();
		if (StringUtils.isNotBlank(deptId)) {
			Long deptIdl = Long.valueOf(deptId);
			List<Long> childIds = deptService.listChildrenIds(deptIdl);
			childIds.add(deptIdl);
			map.put("deptId", null);
			map.put("deptIds",childIds);
		}
		return fileAuditDao.auditReportList(map);
	}

	@Override
	public int auditReportCount(Map<String, Object> map){
		String deptId = map.get("deptId").toString();
		if (StringUtils.isNotBlank(deptId)) {
			Long deptIdl = Long.valueOf(deptId);
			List<Long> childIds = deptService.listChildrenIds(deptIdl);
			childIds.add(deptIdl);
			map.put("deptId", null);
			map.put("deptIds",childIds);
		}
		return fileAuditDao.auditReportCount(map);
	}
	
}
